1130B - Two Cakes - CodeForces Solution


greedy *1200

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;

// Header files, namespaces, 
// macros as defined above 
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
  
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
  

#define Faster_IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ll long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vint vector<int>
#define vlong vector<ll>
#define vchar vector<char>
#define vbool vector<bool>
#define vstring vector<string>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>> 
#define PR_Q priority_queue
#define sz(var) (int)(var).size()
#define flf(var, start, lim_of_loop) for(ll var=start;var<=lim_of_loop;var++)
#define flr(var, start, lim_of_loop) for(ll var=start;var>=lim_of_loop;var--)
#define mset(var) memset(var, 0, sizeof(var))
#define sorting(var1, var2) sort(var1.begin() + var2, var1.end())
#define all(var1, var2) var1.begin() + var2, var1.end()
#define bitcount(var) __builtin_popcount(var)


#define here cout<<"Here"<<endl
#define haa cout<<"YES"<<endl
#define naa cout<<"NO"<<endl
#define line cout << endl;
#define nl endl
#define endl '\n'
#define sp " "
#define ff first
#define ss second
#define LB lower_bound
#define UB upper_bound
#define llmax LLONG_MAX
#define llmin LLONG_MIN
#define intmax INT_MAX
#define intmin INT_MIN
#define sstream stringstream
#define pf(var) push_front(var)
#define pb(var) push_back(var)
#define pbpr(var1, var2) push_back({var1, var2})
#define modval 1000000007
#define PI acos(-1.0)
#define eps 0.0000001 

template < typename F, typename S >ostream& operator << ( ostream& os, const pair< F, S > & p ) {return os << "(" << p.first << ", " << p.second << ")";}
template < typename T >ostream &operator << ( ostream & os, const vector< T > &v ) {os << "{";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << *it;}return os << "}";}
template < typename T >ostream &operator << ( ostream & os, const set< T > &v ) {os << "[";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << *it;}return os << "]";}
template < typename T >ostream &operator << ( ostream & os, const multiset< T > &v ) {os << "[";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << *it;}return os << "]";}
template < typename F, typename S >ostream &operator << ( ostream & os, const map< F, S > &v ) {os << "[";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << it -> first << " = " << it -> second ;}return os << "]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)
void faltu () {cerr << endl;}
template <typename T>void faltu( T a[], int n ) {for(int i = 0; i < n; ++i) cerr << a[i] << ' ';cerr << endl;}
template <typename T, typename ... hello>void faltu( T arg, const hello &... rest) {cerr << arg << ' ';faltu(rest...);}

bool CMP(pair<ll, ll> &a, pair<ll, ll> &b){
	if(a.ff > b.ff){
		return 1;
	}
	else if(a.ff == b.ff){
		if(a.ss < b.ss){
			return 1;
		}
		else{
			return 0;
		}
	}
	else{
		return 0;
	}
}

bool SPACE(char ch){if(ch == ' '){return true;}else{return false;}}
bool PALINDROME(string s, int sz){for(int i=1, j=sz;i<=(sz/2);i++, j--){if(s[i] != s[j]){return false;}}return true;}
bool PRIME(ll n){if(n < 2){return false;}if(n == 2){return true;}for(ll ii = 2; ii * ii <= n; ii++){if(n % ii == 0){return false;}}return true;}


ll LEN(ll x){ll res=0;while(x!=0){res++;x/=10;}return res;}
ll CEIL(ll n, ll x){if(n % x == 0){return (n / x);}else{return ((n / x) + 1);}}
ll GCD(ll a, ll b){if (b == 0){return a;}return GCD(b, a % b);}
ll LCM(ll a, ll b){return (a / GCD(a, b)) * b;}
ll FACTORIAL(ll a){if(a == 0 || a == 1){return 1;}else{return a * FACTORIAL(a - 1);}}
ll COMBINATION(ll n, ll r){ll res = 0;if (r == 0) {return 1;}else{res = COMBINATION(n, r - 1) * (n - r + 1) / r;}return res;}
ll SOD(ll x){ll res = 0;while(x != 0){res += (x % 10); x /= 10;}return res;}
ll SUBSTRACT(ll a, ll b, ll mod){return (a%mod-b%mod+mod)%mod;}

/// Base Changer ///
char reVal(ll num){if (num >= 0 && num <= 9){return (char)(num + '0');}else{return (char)(num - 10 + 'A');}}
string BASE_CHANGER(ll inputNum, ll base){ll index = 0;string res = "";while (inputNum > 0) {res.push_back(reVal(inputNum % base));index++;inputNum /= base;}reverse(res.begin(), res.end());return res;}

void STACK_Show(stack<int> temp){while(!temp.empty()){cout << temp.top() << sp; temp.pop();}cout << endl;}

void Q_Show(queue<int> temp){while(!temp.empty()){cout << temp.front() << sp; temp.pop();}cout << endl;}
void DQ_Show(deque<int> temp){while(!temp.empty()){cout << temp.front() << sp; temp.pop_front();}cout << endl;}
void PRQ_SHOW(PR_Q<ll> temp){while(!temp.empty()){cout << temp.top() << sp; temp.pop();}cout << endl;}

void Q_Pair_Show(queue<pii>temp){while(!temp.empty()){cout << temp.front().ff << sp << temp.front().ss << endl;temp.pop();}}
void PRQ_Pair_Show(PR_Q<pll, vector<pll>, greater<pll>> temp){while(!temp.empty()){cout << temp.top().ff << sp << temp.top().ss << endl;temp.pop();}cout << endl;}


int dx[]  = {  1, -1,  0,  0,  1, -1,  1, -1};
int dy[]  = {  0,  0,  1, -1,  1, -1, -1,  1};
int knx[] = { -2, -2,  2,  2, -1,  1, -1,  1};
int kny[] = { -1,  1, -1,  1, -2, -2,  2,  2};



vector<vector<ll>> g;
vector<ll> par, need, dp;
vector<char> dir;
vector<bool> vis;

void solve(){
	ll n; cin >> n;

	vlong arr(2 * n);

	vpll vpr(n + 1, {-10, -10});

	for(int i = 0; i < 2 * n; i++){
		cin >> arr[i];

		if(vpr[arr[i]].ff == -10){
			vpr[arr[i]].ff = i;
		}
		else{
			vpr[arr[i]].ss = i;
		}
	}

	pll cur = {0, 0};

	ll ans = 0;

	for(int i = 1; i <= n; i++){
		ll dis1 = abs(cur.ff - vpr[i].ff) + abs(cur.ss - vpr[i].ss);
		ll dis2 = abs(cur.ff - vpr[i].ss) + abs(cur.ss - vpr[i].ff);

		if(dis1 <= dis2){
			ans += dis1;
		}
		else{
			ans += dis2;
		}

		cur = vpr[i];
	}

	cout << ans << endl;

}


int main ()
{
	Faster_IO;
	int tc; tc = 1;
	// int tc; cin >> tc;
	// cout << tc << endl;
	flf(iii, 1, tc){
		// cout << "Case " << iii << ": " << endl;
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies